package com.fxf.srb.core.controller.admin;

import com.fxf.common.exception.Assert;
import com.fxf.common.exception.BusinessException;
import com.fxf.common.result.R;
import com.fxf.common.result.ResponseEnum;
import com.fxf.srb.core.pojo.entity.IntegralGrade;
import com.fxf.srb.core.service.IIntegralGradeService;
import com.fxf.srb.core.service.impl.IntegralGradeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 饭小范
 * @version 1.0
 * @description: TODO
 * @date 2021/5/26 22:40
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/core/integralGrade")
@Slf4j
public class AdminIntegralGradeController {

    @Resource
    private IIntegralGradeService iIntegralGradeService;

    @GetMapping("/list")
    @ApiOperation("积分等级列表")
    public R listAll(){

        log.info("hi i'm helen");
        log.warn("warning!!!");
        log.error("it's a error");
        List<IntegralGrade> list = iIntegralGradeService.list();
        return R.ok().data("list",list);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "根据id删除积分等级", notes = "逻辑删除")
    public R removeById(@PathVariable Long id){
        boolean result = iIntegralGradeService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    @PostMapping("/save")
    @ApiOperation("新增积分等级")
    public R save(@RequestBody IntegralGrade integralGrade){

        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean result = iIntegralGradeService.save(integralGrade);
        if (result){
            return R.ok().message("添加成功");
        }else {
            return R.error().message("报错失败");
        }
    }

    @GetMapping("/get/{id}")
    @ApiOperation("根据id获取积分等级")
    public R getById(@PathVariable Long id){
        IntegralGrade integralGrade = iIntegralGradeService.getById(id);
        if (integralGrade != null){
            return R.ok().data("record",integralGrade);
        }else {
            return R.error().message("数据不存在");
        }
    }

    @PutMapping("/update")
    @ApiOperation("根据id修改积分等级")
    public R updateById(@RequestBody IntegralGrade integralGrade){
        boolean b = iIntegralGradeService.updateById(integralGrade);
        if (b){
            return R.ok().message("更新成功");
        }else {
            return R.ok().message("更新失败");
        }

    }
}
